//
//  QuestionChooseView.m
//  Learn
//
//  Created by 迟建国 on 2024/4/4.
//  Copyright © 2024 迟建国. All rights reserved.
//

#import "QuestionChooseView.h"
#import "QuestionTableViewCell.h"

@interface QuestionChooseView ()<UITableViewDelegate, UITableViewDataSource>
@property (nonatomic, strong) UITableView *tableView;

@end

@implementation QuestionChooseView

+ (QuestionChooseView *)chooseQuestionWithVc:(UIViewController *)vc Complection:(void(^)(NSString *str))block {
    QuestionChooseView *  inputView;
    inputView = [[QuestionChooseView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_W, 345 + SafeAreaBottomHeight)];
    [inputView initUI];
    inputView.chooseBlock = block;
    
    NSLog(@"%@", NSObject.getKeyWindowRootViewController);
    [NSObject.getKeyWindowRootViewController gq_presentFromeBottomView:inputView];
    NSObject.getKeyWindowRootViewController.gq_enableBlankDismiss = YES;
    
    return inputView;
}

- (void)initUI {
    UIView *bgView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_W, 345 + SafeAreaBottomHeight)];
    bgView.backgroundColor = [UIColor whiteColor];
    [self addSubview:bgView];
    
    self.tableView=[[UITableView alloc]initWithFrame:CGRectMake(0,5, SCREEN_W, 285) style:UITableViewStylePlain];
    [bgView addSubview:self.tableView];
    if (@available(iOS 13.0, *)) {
        [self.tableView setAutomaticallyAdjustsScrollIndicatorInsets:NO];
    } else {
        // Fallback on earlier versions
    }
    if (@available(iOS 15.0, *)) {
        self.tableView.sectionHeaderTopPadding = 0;
    } else {
        // Fallback on earlier versions
    }
    if (@available(iOS 11.0, *)) {
        self.tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
    }
    self.tableView.backgroundColor=[UIColor clearColor];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableView.delegate=self;
    self.tableView.dataSource=self;
    self.tableView.showsVerticalScrollIndicator = NO;
    self.tableView.showsHorizontalScrollIndicator = NO;
    [self.tableView registerNib:[UINib nibWithNibName:@"QuestionTableViewCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:@"QuestionTableViewCell"];
    
    UIButton *doneBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    doneBtn.layer.cornerRadius = 22.5;
    doneBtn.layer.masksToBounds = YES;
    doneBtn.backgroundColor = rgba(41, 241, 156, 1);
    [doneBtn setTitle:@"完成" forState:UIControlStateNormal];
    [doneBtn setTitleColor:UIColor.whiteColor forState:UIControlStateNormal];
    doneBtn.titleLabel.font = [UIFont fontWithName:@"PingFangSC-Semibold" size:18];
    [doneBtn addTarget:self action:@selector(done) forControlEvents:UIControlEventTouchUpInside];
    [bgView addSubview:doneBtn];
    [doneBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(bgView.mas_left).offset(10);
        make.right.equalTo(bgView.mas_right).offset(-10);
        make.bottom.equalTo(bgView.mas_bottom).offset(-SafeAreaBottomHeight - 10);
        make.height.offset(45);
    }];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 4;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 52;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    QuestionTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"QuestionTableViewCell" forIndexPath:indexPath];
    if (!cell) {
        cell = [[[NSBundle mainBundle]loadNibNamed:@"QuestionTableViewCell" owner:self options:nil] lastObject];
    }
    return cell;
}
- (void)done {
    [NSObject.getKeyWindowRootViewController gq_removeBgView];
}
@end
